package com.axis2.common.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author yemulin
 * @create 2012-8-20 上午10:49:57
 */
public class HtmlForm {

	private static final String name = "sendOrder";
	private static final String id = "sendOrder";
	private static final String method = "post";

	private String autoSubmitScritp = "<script language='javascript'>setTimeout(\"document.getElementById('sendOrder').submit();\",500);</script>";

	Map<String, String> inputMap = new LinkedHashMap<String, String>();

	Map<String, String> headAttributeMap = new LinkedHashMap<String, String>();

	public HtmlForm() {
		// 默认头属性
		headAttributeMap.put("name", name);
		headAttributeMap.put("id", id);
		headAttributeMap.put("method", method);
	}

	// 向form 头添加一个属性元素
	public void addAttribute(String name, String value) {
		headAttributeMap.put(name, value);
	}

	// 从form 头清除一个属性元素
	public void removeAttribute(String name) {
		headAttributeMap.remove(name);
	}

	// 向form添加一个隐藏域元素
	public void addInput(String name, String value) {
		inputMap.put(name, value);
	}

	// 向form添加一个隐藏域元素
	public void removeInput(String name) {
		inputMap.remove(name);
	}

	public String getFormStr() {
		StringBuilder formStr = new StringBuilder();
		// form开始
		formStr.append("<form ");

		// 添加头属性
		Iterator<Entry<String, String>> headIt = headAttributeMap.entrySet().iterator();
		while (headIt.hasNext()) {
			Entry<String, String> inputEntry = headIt.next();
			formStr.append(inputEntry.getKey()).append("=\"").append(inputEntry.getValue()).append("\" ");
		}
		formStr.append(">");
		// 添加隐藏域
		Iterator<Entry<String, String>> it = inputMap.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, String> inputEntry = it.next();
			formStr.append("<input type=\"hidden\" name=\"").append(inputEntry.getKey()).append("\" value=\"")
					.append(inputEntry.getValue()).append("\">");
		}
		// form结束
		formStr.append("</form>");
		// 添加自动提交脚步
		formStr.append(autoSubmitScritp);

		return formStr.toString();
	}

	public String getName() {
		return name;
	}

	public String getAutoSubmitScritp() {
		return autoSubmitScritp;
	}

	public void setAutoSubmitScritp(String autoSubmitScritp) {
		this.autoSubmitScritp = autoSubmitScritp;
	}

}
